package com.example.libraryManager.controller;


import com.example.libraryManager.form.SearchForm;
import com.example.libraryManager.service.impl.BooksService;
import com.example.libraryManager.utils.ResultUtils;
import com.example.libraryManager.vo.PageVo;
import com.example.libraryManager.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2022-09-02
 */
@RestController
@RequestMapping("/books")
public class BooksController {

    @Autowired
    private BooksService booksService;

    /**
     * 分页查询
     * @param size 每一页的数据数量
     * @param page 当前第几页
     * @return
     */
    @GetMapping("/pageList/{page}/{size}")
    public ResultVo pageList(
            @PathVariable("size") Integer size,
            @PathVariable("page") Integer page
    ){
        PageVo pageVo=booksService.pageList(page,size);
        if (pageVo==null){
            return ResultUtils.failed(1);
        }
        return ResultUtils.success(pageVo);
    }

    /**
     * 搜索 + 分页
     * @param searchForm
     * @return
     */
    @GetMapping("/search")
    public ResultVo pageListSelect(SearchForm searchForm){
        PageVo pageVo=booksService.search(searchForm);
        return ResultUtils.success(pageVo);
    }
}

